import { Strategy } from 'passport-local';
import { PassportStrategy } from '@nestjs/passport';
import { Injectable, UnauthorizedException,HttpException, HttpStatus } from '@nestjs/common';
import { AuthService } from './auth.service';

@Injectable()
export class LocalStrategy extends PassportStrategy(Strategy) {
  constructor(private authService: AuthService) {
    super();
  }

  async validate(username: string, password: string): Promise<any> {
     console.log('校验用户：')
    const user = await this.authService.validateUser(username, password);
     console.log('校验用户--1：',user)
    if (!user) {
      // throw new UnauthorizedException();
       throw new HttpException('用户名或者密码错误！', HttpStatus.UNAUTHORIZED);
    }
    console.log('校验用户：',user)
    return user;
  }
}